//
//  NewHomeInfoHeaderView.m
//  GC_OC
//
//  Created by  Quan He on 2022/4/9.
//

#import "NewHomeInfoHeaderView.h"
@interface NewHomeInfoHeaderView()<CAAnimationDelegate>
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *hideButton;
@property (nonatomic, assign) BOOL animating;
@property (nonatomic, assign) CGFloat fromValue;
@end
@implementation NewHomeInfoHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.00];
        self.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.00];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.backView];
        [self.contentView addSubview:self.titleLabel];
        self.fromValue = 0;
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-13);
            make.left.equalTo(@(36));
        }];
        [self.contentView addSubview:self.hideButton];

        [self.hideButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(15));
            make.right.equalTo(@(-36));
            make.center.equalTo(self.titleLabel);
        }];
        self.hideButton.hitTestEdgeInsets = UIEdgeInsetsMake(-12, -12, -12, -12);

    }
    return self;
}
- (void)setHide:(BOOL)hide {
    _hide = hide;
    self.hideButton.selected = hide;
    [self isHide:hide animate:NO];
}

- (void)tapTitle
{
    [self showAction];
}

- (void)showAction {
    if (self.animating) {
        return;
    }
    self.animating = YES;
    self.hideButton.selected = !self.hideButton.isSelected;
    _hide = self.hideButton.selected;
    self.hideAction(self.hideButton.isSelected);
    [self isHide:self.hide animate:YES];
}

- (void)isHide:(BOOL)isHide animate:(BOOL)animate{
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotateAnimation.duration = animate ? 0.3 : 0;
    rotateAnimation.removedOnCompletion = NO;
    rotateAnimation.fillMode = kCAFillModeForwards;
    rotateAnimation.repeatCount = 1;
    rotateAnimation.delegate = self;
    [self.hideButton.layer removeAllAnimations];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:self.fromValue];
    if (isHide) {
        rotateAnimation.toValue = [NSNumber numberWithFloat:-0.5*M_PI];
        self.fromValue = -0.5*M_PI;
    }else {
        rotateAnimation.toValue = [NSNumber numberWithFloat:0];
        self.fromValue = 0;
    }
    [self.hideButton.layer addAnimation:rotateAnimation forKey:@"animation0"];
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
        _titleLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTitle)];
        [_titleLabel addGestureRecognizer:tap];
    }
    return _titleLabel;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(16, 12, SCREEN_WIDTH - 32, 50)];
        _backView.backgroundColor = [UIColor whiteColor];
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_backView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(2, 2)];
        layer.path = path.CGPath;
        _backView.layer.mask = layer;
    }
    return _backView;
}

- (UIButton *)hideButton {
    if (!_hideButton) {
        _hideButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.adjustsImageWhenHighlighted = NO;
            [button setImage:[UIImage imageNamed:@"xiangxia"] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(showAction) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _hideButton;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.animating = NO;
}

@end
